package com.sulj.domain;

import com.sulj.enums.CouponType;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * 优惠券类
 *
 * @author slj
 * @date 2025-03-29
 */
public abstract class Coupon {

    /**
     * 券编码
     */
    protected String code;

    /**
     * 折扣类型
     */
    protected CouponType discountType;

    /**
     * 折扣值
     */
    protected BigDecimal discountValue;

    /**
     * 最低使用门槛 单位：元
     */
    private BigDecimal minAmount;

    public Coupon(CouponType discountType, String code, BigDecimal minAmount, BigDecimal discountValue) {
        this.discountType = Objects.requireNonNull(discountType, "优惠券类型不能为空");
        this.code = Objects.requireNonNull(code, "券编码不能为空");
        this.discountValue = Objects.requireNonNull(discountValue, "折扣值不能为空");
        this.minAmount = minAmount;
    }


    /**
     * 计算优惠价格
     *
     * @param order 订单
     * @return 优惠后的价格
     */
    public abstract BigDecimal applyDiscount(Order order);

    //校验金额是否满足优惠条件
    public boolean validate(BigDecimal totalAmount) {
        return minAmount == null || totalAmount.compareTo(minAmount) >= 0;
    }
}
